//
//  WangYiViewController.m
//  UI13_UITableViewCellHomework
//
//  Created by dllo on 16/6/22.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "WangYiViewController.h"
#import "Duanzi.h"
#import "LabelTableViewCell.h"
#import "ImageViewAndLabelTableViewCell.h"
#import "GetHeightTools.h"
@interface WangYiViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain) NSMutableArray *duanziArray;
@property (nonatomic,retain) UITableView *tableView;

@end

@implementation WangYiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"网易";
    [self config];
    
    [self handleData];
    [self createView];
}

-(void)config{
    self.duanziArray = [NSMutableArray arrayWithCapacity:5];
    
}


-(void)handleData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"duanzi" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    
    NSArray *array  = [dictionary valueForKey:@"段子"];
    
    
    for(NSDictionary *dic in array){
        Duanzi *duanzi = [[Duanzi alloc] init];
        
        [duanzi setValuesForKeysWithDictionary:dic];
        
        [self.duanziArray addObject:duanzi];
    }
    
}

-(void)createView{
    /** 顶部条预留位置 */
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    topView.backgroundColor = [UIColor orangeColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height - 40) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.tableView registerClass:[LabelTableViewCell class] forCellReuseIdentifier:@"labelCellReuse"];
    /** 第二个重用池 */
    [self.tableView registerClass:[ImageViewAndLabelTableViewCell class] forCellReuseIdentifier:@"imageCellReuse"];
    
    /** 暂时固定行宽 */
//    self.tableView.rowHeight = 600.0;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.duanziArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    Duanzi *duanzi  = [self.duanziArray objectAtIndex:indexPath.row];
    if (duanzi.img){
       
        CGFloat height = [GetHeightTools imageHeightWithImageUrl:duanzi.img];
        
        CGFloat heightOfReturn = [GetHeightTools heightWith:duanzi.digest];
        
        return heightOfReturn + height + 20;
    } else {
//        NSLog(@"%ld", indexPath.row);
        return [GetHeightTools heightWith:duanzi.digest] + 20.0;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Duanzi *duanzi = [self.duanziArray objectAtIndex:indexPath.row];
    
    
    if (duanzi.img){
        ImageViewAndLabelTableViewCell *tabelViewCell = [tableView dequeueReusableCellWithIdentifier:@"imageCellReuse" forIndexPath:indexPath];
        tabelViewCell.duanzi = duanzi;
        return tabelViewCell;
    } else {
        LabelTableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"labelCellReuse" forIndexPath:indexPath];
        tableViewCell.duanzi = duanzi;
   //     NSLog(@"%ld", indexPath.row);
        return tableViewCell;
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
